//
//  NSString+Topic.m
//  HolyShit
//
//  Created by Robin on 2016/10/12.
//  Copyright © 2016年 linbei. All rights reserved.
//

#import "NSString+Topic.h"

@implementation NSString (Topic)

+ (NSMutableString *)spaceStringForTagIconWithIconNumber:(NSInteger)iconNumber
{
    NSMutableString *str = [NSMutableString new];
    if(iconNumber > 0){
        for (NSInteger i = 0; i < iconNumber; ++i){
            [str appendString:@"    "];
        }
        if(IOS9_OR_LATER){
            [str appendString:iconNumber > 2 ? @"  " : @" "];
        }
    }
    return str;
}

- (CGSize)communityImageSize
{
    CGFloat imgMaxWid = ScreenWidth - 20;
    CGSize orignSize = [self my_lastComponentOriginalImageSize];
    if (CGSizeEqualToSize(CGSizeZero, orignSize) || orignSize.height == 0 || orignSize.width == 0){
        orignSize = CGSizeMake(imgMaxWid, imgMaxWid / 2);
    }
    if (orignSize.width < imgMaxWid / 2) {
        //小于最大就显示原图
        return orignSize;
    }
    CGSize size = [self my_sizeWithChangeWidth:orignSize width:imgMaxWid];
    if (size.height > 100000){
        size.height = 150;
    }
    if (CGSizeEqualToSize(CGSizeZero, size) || size.height == 0 || size.width == 0){
        size = CGSizeMake(imgMaxWid, 150);
    }
    return size;
}

-(CGSize)my_lastComponentOriginalImageSize
{
    NSArray* targetArray = [self componentsSeparatedByString:@"_"];
    if(targetArray.count >= 2){
        NSArray *tmpArray = [targetArray subarrayWithRange:NSMakeRange(targetArray.count - 2, 2)];
        int width = 0;
        int height = 0;
        NSString *string1 = [tmpArray lastObject];
        if([string1 containsString:@"x"]){
            NSArray* taobaoArray = [string1 componentsSeparatedByString:@"x"];
            width = [taobaoArray[0] intValue];
            height = [taobaoArray[1] intValue];
        }
        else{
            NSString *string2 = [tmpArray firstObject];
            height = string1.intValue;
            width = string2.intValue;
        }
        
        if (isnan(width) || isnan(height)){
            width = 0;
            height = 0;
        }
        return CGSizeMake(width, height);
    }
    return CGSizeZero;
}

- (CGSize)my_sizeWithChangeWidth:(CGSize)size width:(CGFloat)width
{
    if(size.width > 0 && size.height > 0){
        CGSize newSize = CGSizeMake(ceil(width), ceil(size.height * (width / size.width)));
        return newSize;
    }
    return CGSizeZero;
}

@end
